package com.clouddrive;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {

    public static HikariDataSource dataSource;

    static {
        try {
          Properties properties=new Properties();
          InputStream inputStream=JdbcUtils.class.getClassLoader().getResourceAsStream("hikaricp.properties");
          properties.load(inputStream);
          HikariConfig config=new HikariConfig(properties);
          dataSource=new HikariDataSource(config);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取数据库连接池连接
     * @return 返回null，连接失败
     */
    public static Connection getConnetion(){
        Connection conn = null;
        try {
            conn=dataSource.getConnection();
            //System.out.println("success");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
